package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api("购物车相关接口")
@RequestMapping("/user/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * 购物车新增
     * */
    @ApiOperation("添加商品到购物车")
    @PostMapping("/add")
    public Result addShoppingCart(@RequestBody ShoppingCartDTO dto) {
        log.info("新增购物车{}",dto);
        shoppingCartService.addCart(dto);
        return Result.success();
    }
    /**
     * 查询购物车列表（需要根据用户id查询）
     * */
    @ApiOperation("查询购物车列表")
    @GetMapping("/list")
    public Result listShoppingCart() {
        log.info("查询购物车");
        List<ShoppingCart> list = shoppingCartService.list();
        return Result.success(list);
    }
    /**
     * 清空购物车
     * */
    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result cleanShoppingCart() {
        log.info("清空购物车");
        shoppingCartService.cleanCart();
        return Result.success();
    }
    /**
     * 删除一个购物车中的商品
     * */
    @PostMapping("/sub")
    public Result subShoppingCart(@RequestBody ShoppingCartDTO dto) {
        log.info("删除购物车中的商品{}",dto);
        shoppingCartService.sub(dto);
        return Result.success();
    }
}
